import NProgress from 'nprogress'
import { Router } from 'vue-router'
import { TOKEN } from '@/constants/constants'
import { getStorage } from '@/utils/localstorage'
import useHttpRequest from '@/store/modules/httpRequest'

export default function setupUserLoginGuard(router: Router) {
  router.beforeEach(async (to, _, next) => {
    NProgress.start()
    if (to.path !== '/') {
      const httpRequest = useHttpRequest()
      httpRequest.removeHttpRequestList()
    }
    if (!getStorage(TOKEN)) {
      if (to.name !== 'login' && to.name !== 'error') {
        next('/login')
        return
      }
    }
    next()
  })
}
